package com.kate.mall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.kate.mall.common.constants.RabbitMessageConstants.*;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/9/3 15:29
 */
@Configuration
public class NdmallRabbitMQConfig {

    /**
     * 使用JOSON序列化机制，进行消息转换
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /*TODO TTL消息 定时*/
    public static class OrderEventConfig{
        /*TODO 1.order-event-exchange交换机*/
        @Bean
        public TopicExchange orderEventExchange(){
            return new TopicExchange(ORDER_EVENT_EXCHANGE,true,false);
        }
        /*TODO 2.2个队列*/

        /*2.1.死信队列-order.close.queue*/
        @Bean
        public Queue orderCloseQueue(){
            return new Queue(ORDER_CLOSE_QUEUE,true,false,false);
        }
        /*2.2.TTL队列-order.create.queue*/
        @Bean
        public Queue orderCreateQueue(){
            return QueueBuilder.durable(ORDER_CREATE_QUEUE)
                    .exclusive()
                    .ttl(2*60*1000) //模拟2分钟   真实值 30分钟 1000 * 60 *30
                    .deadLetterExchange(ORDER_EVENT_EXCHANGE)
                    .deadLetterRoutingKey(ORDER_CLOSE_DLK)
                    .build();

        }

        /*2.3.释放优惠券资源的队列-order.close.coupon.queue*/
        @Bean
        public Queue orderCloseOtherQueue(){
            return new Queue(ORDER_CLOSE_COUPON_QUEUE,true,false,false);
        }
        /*TODO 3.2个队列绑定到交换机*/
        @Bean
        public Binding orderCreateQueueBinding(){
            return BindingBuilder.bind(orderCreateQueue()).to(orderEventExchange()).with(ORDER_CREATE_BINDING_KEY);
        }

        @Bean
        public Binding orderCloseBinding(){
            return BindingBuilder.bind(orderCloseQueue()).to(orderEventExchange()).with(ORDER_CLOSE_BINDING_KEY);
        }

        @Bean
        public Binding orderReleaseOtherBinding(){
            return BindingBuilder.bind(orderCloseOtherQueue()).to(orderEventExchange()).with(ORDER_CLOSE_OTHER_BINDING_KEY);
        }

    }
}




















